﻿using Ingr.SP3D.Common.Client.Controls;
using Ingr.SP3D.Common.Middle;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TMPipeTopology;

namespace SWS.PIP.CMD
{
	public partial class ImportSpoolDataDlg : Form
	{
		/// <summary>
		/// 当前选择BO节点下Spool所在的分区及分段
		/// </summary>
		Dictionary<string, List<string>> mAreaAndBlockMap = new Dictionary<string, List<string>>();

		ProgressBarDlg progressDlg = new ProgressBarDlg();

		public ImportSpoolDataDlg()
		{
			InitializeComponent();

			progressDlg.Owner = this;
		}

		private void comboBox_HierarcYy_SelectedIndexChanged(object sender, EventArgs e)
		{
			mAreaAndBlockMap.Clear();

			comboBox_Area.Focus();
			HierarchyComboBox.HierarchyComboBoxItem oComboBoxItem = comboBox_HierarcYy.SelectedItem as HierarchyComboBox.HierarchyComboBoxItem;

			//获取所有Spool
			List<BusinessObject> oSpools = CommonFunctions.GetAllSpoolFromAssmNode(oComboBoxItem.BusinessObject);

			List<TMSpool> spoolInfoList = new List<TMSpool>();
			TMSpool spoolInfo = null;
			List<string> blockList = null;
			foreach (var spool in oSpools)
			{
				spoolInfo = new TMSpool(spool);

				if (mAreaAndBlockMap.ContainsKey(spoolInfo.ZoneOrAreaName))
				{
					blockList = mAreaAndBlockMap[spoolInfo.ZoneOrAreaName];
				}
				else
				{
					blockList = new List<string>();
					mAreaAndBlockMap.Add(spoolInfo.ZoneOrAreaName, blockList);
				}

				if (!blockList.Contains(spoolInfo.BlockName))
				{
					blockList.Add(spoolInfo.BlockName);
				}
			}

			// 			//获取分区和分段对应表
			// 			string sArea = "";
			// 			string sBlock = "";
			// 			
			// 			List<string> blockList = null;
			// 			foreach (var oSpool in oSpools)
			// 			{
			// 				CommonFunctions.GetSpoolAreaAndBlock(oSpool, ref sArea, ref sBlock);
			// 
			// 				if (mAreaAndBlockMap.ContainsKey(sArea))
			// 				{
			// 					blockList = mAreaAndBlockMap[sArea];
			// 				}
			// 				else
			// 				{
			// 					blockList = new List<string>();
			// 					mAreaAndBlockMap.Add(sArea, blockList);
			// 				}
			// 
			// 				if (!blockList.Contains(sBlock))
			// 				{
			// 					blockList.Add(sBlock);
			// 				}
			// 			}

			//设置到Combo
			foreach (var item in mAreaAndBlockMap)
			{
				comboBox_Area.Items.Add(item.Key);

				foreach (var block in item.Value)
				{
					comboBox_Block.Items.Add(block);
				}
			}

			//默认选择第一个
			comboBox_Area.SelectedIndex = 0;
		}

		private void comboBox_Area_SelectedIndexChanged(object sender, EventArgs e)
		{
			comboBox_Block.Items.Clear();
			comboBox_Block.Items.Add("All");

			List<string> blocks = mAreaAndBlockMap[comboBox_Area.SelectedItem.ToString()];
			foreach (var block in blocks)
			{
				comboBox_Block.Items.Add(block);
			}

			comboBox_Block.SelectedIndex = 0;
		}

	}
}
